package com.dtagri.fjwlw.viewpagerfragment;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.dtagri.fjwlw.AppContext;
import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.adapter.ViewPageFragmentAdapter;
import com.dtagri.fjwlw.api.FJwlwApi;
import com.dtagri.fjwlw.base.BaseSimpleFragment;
import com.dtagri.fjwlw.base.TGson;
import com.dtagri.fjwlw.bean.SmallArea;
import com.dtagri.fjwlw.bean.TjAreaMsg;
import com.dtagri.fjwlw.bean.TjCompanyRegion;
import com.dtagri.fjwlw.bean.TjNvr;
import com.dtagri.fjwlw.fragment.DeviceControlFragment;
import com.dtagri.fjwlw.util.HKVideoHelper;
import com.dtagri.fjwlw.widget.EmptyLayout;
import com.dtagri.fjwlw.widget.PagerSlidingTabStrip;
import com.google.gson.reflect.TypeToken;

/**
 * 控制设备1
 * @author wxjn
 * 
 */
public class DeviceControlViewPagerFragment extends BaseSimpleFragment {
	protected PagerSlidingTabStrip mTabStrip;
	protected ViewPager mViewPager;
	protected ViewPageFragmentAdapter mTabsAdapter;

	protected List<TjAreaMsg> tjAreaMsgs = new ArrayList<TjAreaMsg>();

	@Override
	protected void xOnSuccess(byte[] arg2) {
		Type type = new TypeToken<List<TjAreaMsg>>() {
		}.getType();
		tjAreaMsgs = TGson.gson.fromJson(new String(arg2), type);
		

		tjAreaMsgs = trim(tjAreaMsgs);
		if (tjAreaMsgs.size() == 0) {
			baseSimpleActivity.getmErrorLayout().setErrorType(
					EmptyLayout.NODATA);
			baseSimpleActivity.getmErrorLayout().setErrorMessage("该基地暂无控制设备");
		} else {
			for (TjAreaMsg tjAreaMsg : tjAreaMsgs) {
				mTabsAdapter.addTab(tjAreaMsg.getAreaName(),
						tjAreaMsg.getAreaName(), DeviceControlFragment.class,
						getBundle(tjAreaMsg));
			}
		}
	}

	/**
	 * 去除空分区
	 * 
	 * @param tjAreaMsgs
	 * @return
	 */
	private List<TjAreaMsg> trim(List<TjAreaMsg> tjAreaMsgs) {
		List<TjAreaMsg> newTjAreaMsgs = new ArrayList<TjAreaMsg>();
		for (TjAreaMsg tjAreaMsg : tjAreaMsgs) {
			int i = 0;
			for (SmallArea smallArea : tjAreaMsg.getSmallAreas()) {
				i += smallArea.getTjEquipments().size();
			}
			if (i > 0)
				newTjAreaMsgs.add(tjAreaMsg);
		}
		return newTjAreaMsgs;
	}

	private <T extends Serializable> Bundle getBundle(T t) {
		Bundle bundle = new Bundle();
		bundle.putSerializable("data", t);
		return bundle;
	}

	@Override
	public void initView(View view) {
		super.initView(view);
		mTabStrip = (PagerSlidingTabStrip) view
				.findViewById(R.id.pager_tabstrip);

		mViewPager = (ViewPager) view.findViewById(R.id.pager);

		mTabsAdapter = new ViewPageFragmentAdapter(getChildFragmentManager(),
				mTabStrip, mViewPager);

		if (!HKVideoHelper.initeSdk()) {
			getActivity().finish();
			return;
		}
	}

	@Override
	protected void xInitData() {
		baseSimpleActivity.getmErrorLayout().setErrorMessage("加载设备状态中…");
		TjCompanyRegion tjCompanyRegion = AppContext.user.getTjCompanyRegions()
				.get(AppContext.userOperate.getCurBaseIndex());
		FJwlwApi.companyEquipment2(tjCompanyRegion.getRegionId(), mHandler);
	}

	@Override
	protected int getLayoutId() {
		return R.layout.base_viewpage_fragment;
	}

	@Override
	protected String getCacheKey() {
		return null;
	}

}
